<?php

namespace App\Http\Middleware;

use App\Exceptions\InvalidRequestException;
use Closure;
use Illuminate\Support\Facades\Cache;

// 校验token是否过期
class AuthToken
{
    /**
     * @param $request
     * @param Closure $next
     * @return mixed
     * @throws InvalidRequestException
     */
    public function handle($request, Closure $next)
    {
        // token令牌没有过期
        if (!Cache::tags(['token'])->has($request->header('Authorization'))) {
            throw new InvalidRequestException('登录已过期, 请重新登录');
        }

        return $next($request);
    }
}
